package NetworkCopy.IO;

import java.io.*;
import java.net.Socket;

public class ClientIO {
    public static void main(String[] args) throws IOException {
        String host = "localhost";

        File file = new File("data.bin");
        try (
                Socket socket = new Socket(host, 7001);
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream())) {

            System.out.println("Connection established, sending");
            long length = file.length();
            if (length > Integer.MAX_VALUE) {
                System.out.println("File is too large.");
            }
            byte[] bytes = new byte[(int) length];
            int count;

            long start = System.currentTimeMillis();

            while ((count = bis.read(bytes)) > 0) {
                out.write(bytes, 0, count);
            }
            out.flush();

            long end = System.currentTimeMillis();
            System.out.println("Time: " + (end - start) + " ms");
        }
    }
}
